      ******************************************************************
      *PROGRAM : Class roster
      *AUTHOR  : Minh-Tuan Ta
      ******************************************************************
       IDENTIFICATION DIVISION.
       PROGRAM-ID. RPTCLASSROSTER.
       ENVIRONMENT DIVISION.
           SELECT IOFILE       ASSIGN TO "REGMST.DAT"
                               ORGANIZATION    IS INDEXED
                               ACCESS          IS DYNAMIC
                               RECORD KEY      IS IO-REG-ID
                               FILE STATUS     IS WS-STAT.

           SELECT INSCHED      ASSIGN TO "SCHEDMST.DAT"
                               ORGANIZATION    IS INDEXED
                               ACCESS          IS DYNAMIC
                               RECORD KEY      IS SCHED-ID
                               FILE STATUS     IS WS-STAT.

           SELECT INSTU        ASSIGN TO "STUMST.DAT"
                               ORGANIZATION    IS INDEXED
                               ACCESS          IS DYNAMIC
                               RECORD KEY      IS STU-ID
                               ALTERNATE KEY   IS STU-LNAME
                                               WITH DUPLICATES
                               FILE STATUS     IS WS-STAT.

           SELECT INCRS        ASSIGN TO "CRSMST.DAT"
                               ORGANIZATION    IS INDEXED
                               ACCESS          IS DYNAMIC
                               RECORD KEY      IS CRS-ID
                               FILE STATUS     IS WS-STAT.

           SELECT ININST       ASSIGN TO "INSTMST.DAT"
                               ORGANIZATION    IS INDEXED
                               ACCESS          IS DYNAMIC
                               RECORD KEY      IS INST-ID
                               FILE STATUS     IS WS-STAT.

           SELECT INBLDG       ASSIGN TO "BLDGMST.DAT"
                               ORGANIZATION    IS INDEXED
                               ACCESS          IS DYNAMIC
                               RECORD KEY      IS BLDG-ID
                               FILE STATUS     IS WS-STAT.

       DATA DIVISION.
       FILE SECTION.
       FD  IOFILE.
       01  IO-REC.
           05 IO-REG-ID.
               10 IO-TERM     PIC X(6).
               10 IO-CRN      PIC 9(4).
               10 IO-STU-ID   PIC 9(4).
           05  IO-ACTIVE      PIC X.

       FD  INSCHED.
       01  SCHED-REC.
           05  SCHED-ID.
               10  SCHED-TERM     PIC X(6).
               10  SCHED-CRN      PIC 9(4).
           05  SCHED-CRS-ID       PIC X(8).
           05  SCHED-BLDG-CODE    PIC X(11).
           05  SCHED-INST-ID      PIC 9(3).
           05  SCHED-TIME         PIC X(15).
           05  SCHED-ACTIVE       PIC X.

       FD  INSTU.
       01  STU-REC.
           05  STU-ID              PIC 9(4).
           05  STU-FNAME           PIC X(20).
           05  STU-LNAME           PIC X(20).
           05  STU-STREET          PIC X(30).
           05  STU-ZIP             PIC X(5).
           05  STU-ACTIVE          PIC X.

       FD  INCRS.
       01  CRS-REC.
           05  CRS-ID              PIC X(8).
           05  CRS-NAME            PIC X(34).
           05  CRS-HR              PIC 99.
           05  CRS-ACTIVE          PIC X.

       FD  ININST.
       01  INST-REC.
           05  INST-ID             PIC 9(3).
           05  INST-NAME           PIC X(20).
           05  INST-ACTIVE         PIC X.

       FD  INBLDG.
       01  BLDG-REC.
           05  BLDG-ID             PIC X(11).
           05  BLDG-CAPACITY       PIC 9(3).
           05  BLDG-ACTIVE         PIC X.

       WORKING-STORAGE SECTION.
       01  WS-STAT             PIC XX.
       01  WS-EOF              PIC X VALUE 'N'.
       01  WS-TERM             PIC X(6).
       01  WS-CRN              PIC 9(4).
       01  WS-ACTIVE           PIC X.
       01  WS-DATE.
           03  WS-YEAR         PIC 99.
           03  WS-MONTH        PIC 99.
           03  WS-DAY          PIC 99.
       01  WS-ERR              PIC X VALUE 'N'.
       01  WS-CONT             PIC X VALUE 'Y'.
       01  WS-NEXT             PIC X.
       01  WS-COUNTER          PIC 99.

       01  OUT-ARRAY.
       03      OUT-RECORDS OCCURS 20 TIMES.
           05  OUT-STU-ID      PIC 9(4).
           05  FILLER          PIC X VALUE SPACES.
           05  OUT-FNAME       PIC X(20).
           05  FILLER          PIC X VALUE SPACES.
           05  OUT-LNAME       PIC X(20).
           05  FILLER          PIC X VALUE SPACES.
           05  OUT-ZIP         PIC X(5).

       SCREEN SECTION.
       01  INPUT-SCRN.
               05  BLANK SCREEN.
               05  LINE 1  COL 01  VALUE "CLASS ROSTER".
               05          COL 73  PIC 9(2) FROM WS-MONTH.
               05          COL 75  VALUE "/".
               05          COL 76  PIC 9(2) FROM WS-DAY.
               05          COL 78  VALUE "/".
               05          COL 79  PIC 9(2) FROM WS-YEAR.
               05  LINE 2  COL 37  VALUE "CLASS ROSTER".
               05  INPUT-TERM-SCRN.
                   10  LINE 4  COL 20  VALUE "TERM".
                   10          COL 40  PIC X(6) TO WS-TERM.
               05  INPUT-CRN-SCRN.
                   10  LINE 6 COL 20  VALUE "CRN".
                   10          COL 40  PIC 9(4) TO WS-CRN.

               05  LINE 17 COL 34  VALUE "(ENTER XXX TO TERM TO EXIT)".

       01  DISPLAY-SCRN.
       03  BLANK SCREEN.
       03  DISPLAY-REC.
           05  LINE 1  COL 01  VALUE "CLASS ROSTER".
           05          COL 73  PIC 9(2) FROM WS-MONTH.
           05          COL 75  VALUE "/".
           05          COL 76  PIC 9(2) FROM WS-DAY.
           05          COL 78  VALUE "/".
           05          COL 79  PIC 9(2) FROM WS-YEAR.
           05          COL 34  VALUE "CLASS ROSTER".
           05  LINE 2  COL 1   VALUE "CLASS: ".
           05          COL 10  PIC X(34) FROM CRS-NAME.
           05          COL 45  VALUE "TERM: ".
           05          COL 52  PIC X(6) FROM IO-TERM.
           05  LINE 4  COL 1   PIC X(52) FROM  OUT-RECORDS(1).
           05  LINE 5  COL 1   PIC X(52) FROM  OUT-RECORDS(2).
           05  LINE 6  COL 1   PIC X(52) FROM  OUT-RECORDS(3).
           05  LINE 7  COL 1   PIC X(52) FROM  OUT-RECORDS(4).
           05  LINE 8  COL 1   PIC X(52) FROM  OUT-RECORDS(5).
           05  LINE 9  COL 1   PIC X(52) FROM  OUT-RECORDS(6).
           05  LINE 10 COL 1   PIC X(52) FROM  OUT-RECORDS(7).
           05  LINE 11 COL 1   PIC X(52) FROM  OUT-RECORDS(8).
           05  LINE 12 COL 1   PIC X(52) FROM  OUT-RECORDS(9).
           05  LINE 13 COL 1   PIC X(52) FROM  OUT-RECORDS(10).
           05  LINE 14 COL 1   PIC X(52) FROM  OUT-RECORDS(11).
           05  LINE 15 COL 1   PIC X(52) FROM  OUT-RECORDS(12).
           05  LINE 16 COL 1   PIC X(52) FROM  OUT-RECORDS(13).
           05  LINE 17 COL 1   PIC X(52) FROM  OUT-RECORDS(14).
           05  LINE 18 COL 1   PIC X(52) FROM  OUT-RECORDS(15).
           05  LINE 19 COL 1   PIC X(52) FROM  OUT-RECORDS(16).
           05  LINE 20 COL 1   PIC X(52) FROM  OUT-RECORDS(17).
           05  LINE 21 COL 1   PIC X(52) FROM  OUT-RECORDS(18).
           05  LINE 22 COL 1   PIC X(52) FROM  OUT-RECORDS(19).
           05  LINE 23 COL 1   PIC X(52) FROM  OUT-RECORDS(20).
           05  LINE 25 COL 18  VALUE "PRESS ENTER TO CONTINUE, OR 'X
      -                                  "' TO EXIT".
           05          COL 58  PIC X   TO  WS-NEXT AUTO.

       01  DISPLAY-EOF-SCRN.
       03  BLANK SCREEN.
       03  DISPLAY-REC.
           05  LINE 1  COL 01  VALUE "CLASS ROSTER".
           05          COL 73  PIC 9(2) FROM WS-MONTH.
           05          COL 75  VALUE "/".
           05          COL 76  PIC 9(2) FROM WS-DAY.
           05          COL 78  VALUE "/".
           05          COL 79  PIC 9(2) FROM WS-YEAR.
           05          COL 34  VALUE "CLASS ROSTER".
           05  LINE 2  COL 1   VALUE "CLASS: ".
           05          COL 10  PIC X(34) FROM CRS-NAME.
           05          COL 45  VALUE "TERM: ".
           05          COL 52  PIC X(6) FROM IO-TERM.
           05  LINE 4  COL 1   PIC X(52) FROM  OUT-RECORDS(1).
           05  LINE 5  COL 1   PIC X(52) FROM  OUT-RECORDS(2).
           05  LINE 6  COL 1   PIC X(52) FROM  OUT-RECORDS(3).
           05  LINE 7  COL 1   PIC X(52) FROM  OUT-RECORDS(4).
           05  LINE 8  COL 1   PIC X(52) FROM  OUT-RECORDS(5).
           05  LINE 9  COL 1   PIC X(52) FROM  OUT-RECORDS(6).
           05  LINE 10 COL 1   PIC X(52) FROM  OUT-RECORDS(7).
           05  LINE 11 COL 1   PIC X(52) FROM  OUT-RECORDS(8).
           05  LINE 12 COL 1   PIC X(52) FROM  OUT-RECORDS(9).
           05  LINE 13 COL 1   PIC X(52) FROM  OUT-RECORDS(10).
           05  LINE 14 COL 1   PIC X(52) FROM  OUT-RECORDS(11).
           05  LINE 15 COL 1   PIC X(52) FROM  OUT-RECORDS(12).
           05  LINE 16 COL 1   PIC X(52) FROM  OUT-RECORDS(13).
           05  LINE 17 COL 1   PIC X(52) FROM  OUT-RECORDS(14).
           05  LINE 18 COL 1   PIC X(52) FROM  OUT-RECORDS(15).
           05  LINE 19 COL 1   PIC X(52) FROM  OUT-RECORDS(16).
           05  LINE 20 COL 1   PIC X(52) FROM  OUT-RECORDS(17).
           05  LINE 21 COL 1   PIC X(52) FROM  OUT-RECORDS(18).
           05  LINE 22 COL 1   PIC X(52) FROM  OUT-RECORDS(19).
           05  LINE 23 COL 1   PIC X(52) FROM  OUT-RECORDS(20).
           05  LINE 25 COL 18  VALUE "END OF FILE, PRESS ENTER TO CONTIN
      -                                  "UE".
           05          COL 58  PIC X   TO  WS-NEXT AUTO.

       01  ERROR-SCRN.
           03  BLANK SCREEN.
           03  LINE 12 COL 32 VALUE "ERROR".
           03  LINE 14 COL 32 VALUE "FILE STATUS:".
           03          COL 46 PIC XX FROM WS-STAT.
           03  LINE 16 COL 32 VALUE "CONTINUE(Y/N)?".
           03          COL 47 PIC X TO WS-CONT AUTO.

       01  CONTINUE-SCRN.
           03  BLANK SCREEN.
           03  LINE 12 COL 32 VALUE "DO YOU WANT TO CONTINUE?".
           03  LINE 13 COL 32 VALUE "CONTINUE(Y/N)?".
           03          COL 47 PIC X TO WS-CONT AUTO.



       PROCEDURE DIVISION.
       100-MAIN.
           ACCEPT WS-DATE FROM DATE
           OPEN    I-O IOFILE
                   INPUT INSCHED
                   INPUT INSTU
                   INPUT ININST
                   INPUT INCRS
                   INPUT INBLDG

           MOVE SPACES TO WS-CONT

           MOVE SPACES TO IO-TERM
           MOVE ZEROS TO IO-CRN
           MOVE SPACES TO SCHED-CRS-ID
           MOVE SPACES TO CRS-NAME
           MOVE ZEROS TO CRS-HR
           MOVE SPACES TO SCHED-BLDG-CODE
           MOVE ZEROS TO BLDG-CAPACITY
           MOVE SPACES TO INST-NAME
           MOVE SPACES TO SCHED-TIME
           MOVE ZEROS TO IO-STU-ID
           MOVE SPACES TO IO-ACTIVE

           DISPLAY INPUT-SCRN
           ACCEPT INPUT-TERM-SCRN

           PERFORM UNTIL WS-TERM = 'XXX' OR WS-CONT = 'N'

               ACCEPT INPUT-CRN-SCRN

               MOVE SPACES TO IO-TERM
               MOVE ZEROS TO IO-CRN
               MOVE ZEROS TO IO-STU-ID

               MOVE 'N' TO WS-ERR

               START IOFILE KEY >= IO-REG-ID
                   INVALID KEY MOVE 'Y' TO WS-ERR
               END-START

               IF NOT WS-ERR = 'Y'

                   MOVE WS-TERM TO SCHED-TERM
                   MOVE WS-CRN TO SCHED-CRN

                   START INSCHED KEY = SCHED-ID
                       INVALID KEY MOVE 'Y' TO WS-ERR
                   END-START

                   IF NOT WS-ERR = 'Y'

                       READ INSCHED NEXT RECORD
                       END-READ

                       MOVE SCHED-CRS-ID TO CRS-ID

                       START INCRS KEY = CRS-ID
                       END-START

                       READ INCRS NEXT RECORD
                       END-READ

                       MOVE 'N' TO WS-EOF

                       PERFORM UNTIL WS-EOF = 'Y' OR WS-NEXT = 'X'
                           PERFORM 300-FILL-OUTPUT

                           IF WS-EOF = 'N'
                               DISPLAY DISPLAY-SCRN
                               ACCEPT DISPLAY-SCRN
                           ELSE
                               DISPLAY DISPLAY-EOF-SCRN
                               ACCEPT DISPLAY-EOF-SCRN
                           END-IF
                       END-PERFORM

                       DISPLAY CONTINUE-SCRN
                       ACCEPT CONTINUE-SCRN

                   ELSE
                       DISPLAY ERROR-SCRN
                       ACCEPT ERROR-SCRN
                   END-IF

               ELSE
                   DISPLAY ERROR-SCRN
                   ACCEPT ERROR-SCRN
               END-IF

               IF NOT WS-CONT = 'N'
                   MOVE SPACES TO IO-TERM
                   MOVE ZEROS TO IO-CRN
                   MOVE SPACES TO SCHED-CRS-ID
                   MOVE SPACES TO CRS-NAME
                   MOVE ZEROS TO CRS-HR
                   MOVE SPACES TO SCHED-BLDG-CODE
                   MOVE ZEROS TO BLDG-CAPACITY
                   MOVE SPACES TO INST-NAME
                   MOVE SPACES TO SCHED-TIME
                   MOVE ZEROS TO IO-STU-ID
                   MOVE SPACES TO IO-ACTIVE

                   DISPLAY INPUT-SCRN
                   ACCEPT INPUT-TERM-SCRN
               END-IF

           END-PERFORM

           CLOSE IOFILE
           CLOSE INSCHED
           CLOSE INSTU
           CLOSE ININST
           CLOSE INCRS
           CLOSE INBLDG

           EXIT PROGRAM.

       200-INIT-OUTPUT.
           MOVE 1 TO WS-COUNTER
           PERFORM UNTIL WS-COUNTER = 20
               MOVE SPACES TO OUT-RECORDS(WS-COUNTER)
               ADD 1 TO WS-COUNTER
           END-PERFORM
           MOVE 1 TO WS-COUNTER
           MOVE 'N' TO WS-EOF.

       300-FILL-OUTPUT.
           PERFORM 200-INIT-OUTPUT

           READ IOFILE NEXT RECORD
               AT END MOVE 'Y' TO WS-EOF
           END-READ

           PERFORM UNTIL WS-EOF = 'Y' OR WS-COUNTER = 20

               IF IO-TERM = WS-TERM AND IO-CRN = WS-CRN
                   AND IO-ACTIVE = 'Y'

                   MOVE IO-TERM TO SCHED-TERM
                   MOVE IO-CRN TO SCHED-CRN

                   START INSCHED KEY = SCHED-ID
                   END-START

                   READ INSCHED NEXT RECORD
                   END-READ

                   MOVE IO-STU-ID TO STU-ID

                   START INSTU KEY = STU-ID
                   END-START

                   READ INSTU NEXT RECORD
                   END-READ

                   MOVE SCHED-BLDG-CODE TO BLDG-ID

                   MOVE STU-ID TO OUT-STU-ID(WS-COUNTER)
                   MOVE STU-FNAME TO OUT-FNAME(WS-COUNTER)
                   MOVE STU-LNAME TO OUT-LNAME(WS-COUNTER)
                   MOVE STU-ZIP TO OUT-ZIP(WS-COUNTER)

                   ADD 1 TO WS-COUNTER
               END-IF

               READ IOFILE NEXT RECORD
                   AT END MOVE 'Y' TO WS-EOF
               END-READ

           END-PERFORM.
